{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7f4fe27b",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'f' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [3]\u001b[0m, in \u001b[0;36m<cell line: 14>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     12\u001b[0m f_01 \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mopen\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mday4.jpg\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mrb\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m     13\u001b[0m f_02 \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mopen\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mday2.jpg\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mrb\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m---> 14\u001b[0m image_file1 \u001b[38;5;241m=\u001b[39m base64\u001b[38;5;241m.\u001b[39mb64encode(\u001b[43mf\u001b[49m\u001b[38;5;241m.\u001b[39mread())\n\u001b[0;32m     15\u001b[0m image_file2 \u001b[38;5;241m=\u001b[39m base64\u001b[38;5;241m.\u001b[39mb64encode(f\u001b[38;5;241m.\u001b[39mread())\n\u001b[0;32m     17\u001b[0m \u001b[38;5;66;03m# 2.酬载准备\u001b[39;00m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'f' is not defined"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "Face++ 人脸比对\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "# 二进制方式打开图片文件\n",
    "f_01 = open('day4.jpg', 'rb')\n",
    "f_02 = open('day2.jpg', 'rb')\n",
    "image_file1 = base64.b64encode(f.read())\n",
    "image_file2 = base64.b64encode(f.read())\n",
    "\n",
    "# 2.酬载准备\n",
    "payload = {\n",
    "    'api_key':'-5I_nvl7_hGNr9JZHdolud3YcGRpfMqV',\n",
    "    'api_secret':'cVl59_o8JqsSuBzKuyC9_BAO5bNeErnj',\n",
    "    'image_base64':img,\n",
    "    'return_attributes':'gender,age,smiling,emotion,beauty'\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())\n",
    "else:\n",
    "    print(response.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7ae27513",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "2f785e60",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1679281710,c1a9ae71-da1c-4b3a-bb3c-ed97fc1624b2', 'time_used': 323, 'confidence': 89.024, 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}, 'faces1': [{'face_token': '1c14968d548aacbbf31b2c3a0748b2bd', 'face_rectangle': {'top': 62, 'left': 80, 'width': 68, 'height': 68}}], 'faces2': [{'face_token': '53c27c24ee00e3864e2b8df118d9867a', 'face_rectangle': {'top': 69, 'left': 75, 'width': 111, 'height': 111}}], 'image_id1': 'BZX/QP8nWKpH5lnU+JfW8Q==', 'image_id2': '3VycDsXne0Jnmy4PS7lnCg=='}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "Face++ 人脸比对\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "# 二进制方式打开图片文件\n",
    "f_01 = open('Y1.jfif', 'rb')\n",
    "f_02 = open('Y2.jfif', 'rb')\n",
    "image_file1 = base64.b64encode(f_01.read())\n",
    "image_file2 = base64.b64encode(f_02.read())\n",
    "\n",
    "# 2.酬载准备\n",
    "payload = {\n",
    "    'api_key':'-5I_nvl7_hGNr9JZHdolud3YcGRpfMqV',\n",
    "    'api_secret':'cVl59_o8JqsSuBzKuyC9_BAO5bNeErnj',\n",
    "    'image_base64_1':image_file1,\n",
    "    'image_base64_2':image_file2,\n",
    "    'return_attributes':'gender,age,smiling,emotion,beauty'\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())\n",
    "else:\n",
    "    print(response.status_code)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69b76bc5",
   "metadata": {},
   "source": [
    "# 人脸搜索"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6064bdd5",
   "metadata": {},
   "source": [
    "## 人脸库的管理"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee96b2c6",
   "metadata": {},
   "source": [
    "### 创建人脸数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "56ec5aa7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n",
      "{'faceset_token': '58c815cd12f93d44b135bbe5979fe641', 'time_used': 208, 'face_count': 0, 'face_added': 0, 'request_id': '1679283802,d2bdb3ff-1cab-486b-b1cd-418664ef4c3c', 'outer_id': '', 'failure_detail': []}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "\n",
    "\n",
    "'''\n",
    "人脸数据库创建\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "# 请求参数\n",
    "\n",
    "params = {\n",
    "    \"api_key\":\"-5I_nvl7_hGNr9JZHdolud3YcGRpfMqV\",\n",
    "    \"api_secret\":\"cVl59_o8JqsSuBzKuyC9_BAO5bNeErnj\",\n",
    "    \"display_name\":\"广州南方学院1栋宿舍楼\"\n",
    "\n",
    "}\n",
    "\n",
    "response = requests.post(request_url, data=params)\n",
    "\n",
    "print (response.status_code)\n",
    "print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "131d10e7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'time_used': 90,\n",
       " 'facesets': [{'faceset_token': '4929180c493e4e278a98cbc2107593b2',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '520af6378dc5360729eb0aaf09c1400d',\n",
       "   'outer_id': '',\n",
       "   'display_name': '广州南方学院1栋宿舍楼',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '58c815cd12f93d44b135bbe5979fe641',\n",
       "   'outer_id': '',\n",
       "   'display_name': '广州南方学院1栋宿舍楼',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '9ee1d443c7ba55e849f708d771d35524',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''}],\n",
       " 'request_id': '1679284110,ef040e32-bc08-47fc-9ef5-a1c60dc187d1'}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "\n",
    "\n",
    "'''\n",
    "人脸数据库创建\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets\"\n",
    "# 请求参数\n",
    "\n",
    "params = {\n",
    "    'api_key':'-5I_nvl7_hGNr9JZHdolud3YcGRpfMqV',\n",
    "    'api_secret':'cVl59_o8JqsSuBzKuyC9_BAO5bNeErnj',\n",
    "    'faceset_token':'58c815cd12f93d44b135bbe5979fe641'\n",
    "\n",
    "}\n",
    "\n",
    "response = requests.post(request_url, data=params)\n",
    "\n",
    "print (response.status_code)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "fdb8d7de",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "400\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'time_used': 78,\n",
       " 'error_message': 'MISSING_ARGUMENTS: faceset_token, outer_id',\n",
       " 'request_id': '1679284362,a5a63c88-0847-434b-b9cf-0181ef791ac1'}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "\n",
    "\n",
    "'''\n",
    "人脸数据库创建\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "\n",
    "# 二进制方式打开图片文件\n",
    "f = open('day4.jpg', 'rb')\n",
    "\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "\n",
    "params = {\n",
    "    'api_key':'-5I_nvl7_hGNr9JZHdolud3YcGRpfMqV',\n",
    "    'api_secret':'cVl59_o8JqsSuBzKuyC9_BAO5bNeErnj',\n",
    "    'image_base64':img,\n",
    "\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "\n",
    "\n",
    "print (response.status_code)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ced014f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "\n",
    "\n",
    "'''\n",
    "人脸数据库创建\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "\n",
    "# 二进制方式打开图片文件\n",
    "f = open('1栋/day4.jpg', 'rb')\n",
    "\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "\n",
    "params = {\n",
    "    'api_key':'-5I_nvl7_hGNr9JZHdolud3YcGRpfMqV',\n",
    "    'api_secret':'cVl59_o8JqsSuBzKuyC9_BAO5bNeErnj',\n",
    "    'faceset_token':\n",
    "    'face_tokens':\n",
    "}\n",
    "\n",
    "\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "\n",
    "\n",
    "print (response.status_code)\n",
    "response.json()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
